﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Reflection;
using IPC.Communication.Framework.Serial;

namespace IPC.Communication.Framework.Profinet.Melsec
{
    public class MelsecFxSerial : SerialDeviceBase
    {
        public bool IsNewVersion
        {
            get;
            set;
        }

        public MelsecFxSerial()
        {
            base.ByteTransform = new RegularByteTransform();
            base.WordLength = 1;
            IsNewVersion = true;
            base.ByteTransform.IsStringReverseByteWord = true;
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return MelsecFxSerialOverTcp.ReadHelper(address, length, ReadFromCoreServer, IsNewVersion);
        }

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return MelsecFxSerialOverTcp.WriteHelper(address, value, ReadFromCoreServer, IsNewVersion);
        }

        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return MelsecFxSerialOverTcp.ReadBoolHelper(address, length, ReadFromCoreServer);
        }

        [HslMqttApi("WriteBool", "")]
        public override OperateResult Write(string address, bool value)
        {
            return MelsecFxSerialOverTcp.WriteHelper(address, value, ReadFromCoreServer);
        }

        //[AsyncStateMachine(typeof(<WriteAsync>d__9))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool value)
        //{
        //    <WriteAsync>d__9 stateMachine = new <WriteAsync>d__9();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("MelsecFxSerial[{0}:{1}]", base.PortName, base.BaudRate);
        }
    }
}
